昨天在文章中提到如何 Dockerize 你的專案,而設定你自己的 Docker Image 就是一件重要的事情,我們可以先看看 Laravel 需要的 PHP 套件,來決定我們要寫的 Dockerfile 內容。
因此我們的 Dockerfile 可以這樣設置。
FROM php:7.3-fpm
RUN apt-get update && apt-get install -y \
# For php gd ext
libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev \
# For php zip ext
zlib1g-dev \
libzip-dev \
# For memcached
libmemcached-dev \
# Install required packages
default-mysql-client \
locales \
# For php composer
unzip \
# For Terminal
git \
subversion \
vim \
&& docker-php-ext-configure mysqli \
&& docker-php-ext-install -j$(nproc) mysqli \
&& docker-php-ext-configure gettext \
&& docker-php-ext-install -j$(nproc) gettext \
&& docker-php-ext-configure gd --with-freetype-dir --with-jpeg \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-configure ldap \
&& docker-php-ext-configure sockets \
&& docker-php-ext-install -j$(nproc) sockets \
&& docker-php-ext-configure pdo_mysql \
&& docker-php-ext-install -j$(nproc) pdo_mysql \
&& docker-php-ext-configure zip \
&& docker-php-ext-install -j$(nproc) zip \
&& pecl install xdebug \
&& docker-php-ext-enable xdebug \
&& pecl install memcached \
&& docker-php-ext-enable memcached
# For composer
RUN curl -sS https://getcomposer.org/installer | php
RUN mv composer.phar /usr/local/bin/composer
# For time zone
ENV TZ=Asia/Taipei
RUN ln -snf /usr/share/zoneinfo/${TZ} /etc/localtime && echo ${TZ} > /etc/timezone
以上有加上一些我自己在設定 container 時需要用到的套件。
Docker Compose 是一個能夠自動建立 docker container 的工具,這樣不用到各個 Dockerfile 目錄中下指令建立 images,也方便管理各個 container
version: docker-compose 版本
services: docker-compose 要運行的 images 設定
php
mysql
完整程式碼
version: "3.3"
services:
php:
build: ./files/php/
restart: unless-stopped
volumes:
- "./html:/var/www/html"
depends_on:
- mysql
environment:
DB_ROOT_PW: ""
mysql:
image: mysql:5.7
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=
ports:
- 3307:3306
volumes:
- ./mysql:/var/lib/mysql
- ./log/mysql:/var/log/mysql
nginx:
image: nginx:latest
ports:
- 81:80
volumes:
- "./html:/var/www/html"
- "./frontend/build:/var/www/html/public"
- "./log/nginx:/var/log/nginx"
- "./files/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
depends_on:
- node
links:
- php
Nginx 設定
server {
listen 80;
listen [::]:80;
server_name localhost;
root /var/www/html/public;
index index.html index.htm
location / {
index index.html index.htm index.php;
location /api {
try_files $uri $uri/ /index.php$request_uri;
}
try_files $uri $uri/ /index.html;
location ~ ^(.+\.php)(.*)$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
# 讓 PHP 可以取得 PATH_INFO 參數
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}